
<template>
  <!--: 数据量过大，过滤改为后端实现 filter-option="filterOption"  option-filter-prop="children"-->
  <a-select
    show-search
    :placeholder="$t('pleaseSelect') + '车型'"
    @search="getData"
    :allow-clear="true"
    :mode="mode"
    :maxTagCount="1"
    :disabled="disabled"
    :filterOption="false"
    v-model="selectedValue"
    @change="changeOption"
  >
    <a-select-option v-for="item in options" :key="item" :value="item">
      {{ item }}
    </a-select-option>
  </a-select>
</template>
<script>
import statisticsApi from '@/api/modules/statistics'
import modelApi from '@/api/modules/system/modelMana'
import lodash from 'lodash'

export default {
  props: {
    defaultValue: {
      type: [Array, String, Object],
      default () {
        return []
      }
    },
    mode: {
      type: String,
      default: 'multiple'
    },
    time: {
      type: Number,
      default: 124578
    },
    disabled: {
      type: Boolean,
      default: false
    },
    query: {
      type: Object,
      default () {
        return null
      }
    }
  },
  data () {
    return {
      selectedValue: [],
      options: []
    }
  },
  watch: {
    defaultValue: {
      handler (val) {
        if (val !== this.selectedValue) {
          this.selectedValue = this.defaultValue.push ? [...this.defaultValue] : this.defaultValue
        }
      },
      immediate: true
    },
    selectedValue (val) {
      if (lodash.isEqual(this.defaultValue, this.selectedValue)) {
        return
      }
      this.$emit('changeoption', val)
      this.getData('')
    },
    time: {
      handler (val) {
        if (val === 0) {
          return
        }
        this.getData()
      },
      immediate: true
    }
  },
  created () {

  },
  methods: {
    getData:lodash.debounce(function(value = '') {
      value = Array.isArray(value) ? '': value
      this.options = []
      modelApi.getMyAllModelCode({ searchWord: value }).then(res => {
          if(res.data){
            res.data.map(item=>{
              this.options.push(item.name)
            })
          }

      })
    }, 500),
    changeOption (val) {
      this.getData()
      this.$emit('changeoption', val)
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    }
  }
}
</script>
